| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Program Files/LibreOffice/help/en-US/text/sbasic/python/ |
Upload File : |
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<base href="../../../../">
<noscript><meta http-equiv="refresh" content="0; URL=../../../../en-US/noscript.html"></noscript>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Python Interactive Shell</title>
<link rel="shortcut icon" href="media/navigation/favicon.ico">
<link type="text/css" href="normalize.css" rel="Stylesheet">
<link type="text/css" href="prism.css" rel="Stylesheet">
<link type="text/css" href="default.css" rel="Stylesheet">
<script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="languages.js"></script><script type="text/javascript" src="en-US/langnames.js"></script><script type="text/javascript" src="flexsearch.debug.js"></script><script type="text/javascript" src="prism.js"></script><script type="text/javascript" src="help2.js" defer></script><script type="text/javascript" src="a11y-toggle.js" defer></script><script type="text/javascript" src="paginathing.js" defer></script><script type="text/javascript" src="en-US/bookmarks.js" defer></script><script type="text/javascript" src="en-US/contents.js" defer></script><script type="text/javascript" src="help.js" defer></script><meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<header id="TopLeftHeader"><a class="symbol" href="en-US/text/shared/05/new_help.html"><div></div></a><a class="logo" href="en-US/text/shared/05/new_help.html"><p dir="auto">LibreOffice 24.2 Help</p></a><div class="dropdowns"><div class="modules">
<button type="button" data-a11y-toggle="modules-nav" id="modules" aria-haspopup="true" aria-expanded="false" aria-controls="modules-nav">Module</button><nav id="modules-nav" hidden=""></nav>
</div></div></header><aside class="leftside"><input id="accordion-1" name="accordion-menu" type="checkbox"><label for="accordion-1" dir="auto">Contents</label><div id="Contents" class="contents-treeview"></div></aside><div id="SearchFrame"><div id="Bookmarks">
<input id="search-bar" type="search" class="search" placeholder="Search in bookmarks for chosen module" dir="auto"><div class="nav-container" tabindex="0"><nav class="index" dir="auto"></nav></div>
</div></div>
<div id="DisplayArea" itemprop="softwareHelp" itemscope="true" itemtype="http://schema.org/SoftwareApplication">
<a name="N0117"></a>
<meta itemprop="keywords" content="Python console">
<meta itemprop="keywords" content="Python Interactive Shell">
<meta itemprop="keywords" content="Python,Shell">
<a name="pythonshell"></a>
<h1 id="N0118" dir="auto">
<a name="pythonshell1"></a>Running Python Interactive Console</h1>
<p id="N0119" class="paragraph" dir="auto">The Python interactive console, also known as Python interpreter or Python shell, provides programmers with a quick way to execute commands and try out and test code without creating a file. UNO objects introspection as well as LibreOffice Python modules documentation can be obtained from the terminal.</p>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id851633601202270" dir="auto">From a full-featured LibreOffice installed package, use either Basic or Python:</p></div>
</div>
<br>
<h2 id="N0121" dir="auto">Using a Basic macro</h2>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Sub interpreter_console
Const UNIX = 4
ps = CreateUnoService("com.sun.star.util.PathSettings")
install_path = ConvertFromURL(ps.Module)
cmd = IIF(GetGuiType()=UNIX,"x-terminal-emulator -e ","")
Shell(cmd + install_path + GetPathSeparator() + "python" )
End Sub
</code></pre></div>
<h2 id="N0127" dir="auto">Using a Python macro</h2>
<div class="pycode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-python line-numbers">
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uno, os, subprocess
def interpreter_console():
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.getServiceManager()
ps = smgr.createInstanceWithContext("com.sun.star.util.PathSettings", ctx)
install_path = uno.fileUrlToSystemPath(ps.Module)
pgm = install_path + os.sep + "python" # Python shell/console path
subprocess.Popen(pgm) # Start Python interactive Shell
</code></pre></div>
<h3 id="N0141" dir="auto">Example output</h3>
<p id="N0142" class="paragraph" dir="auto">
<img src="media/helpimg/python/python_interactive_console.png" class="genericimage" alt="Python Interactive Console" style="width:;height:" itemprop="image" itemscope="true" itemtype="http://schema.org/ImageObject">
</p>
<span id="swlnsyspython_shell_1" class="switch">
<span hidden="true" id="UNIXpython_shell_1" class="UNIX">
<h2 id="hd_id381633446811095" dir="auto">Using the Terminal</h2>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id801633601715910" dir="auto">From a LibreOffice copy included in a GNU/Linux platform, use the terminal as shown:</p></div>
</div>
<br>
<p id="par_id531633444780190" class="paragraph" dir="auto"><span class="literal">whereis</span> or <span class="literal">type</span> terminal commands help locate Python interactive console:</p>
<p id="par_id871633361808210" class="paragraph" dir="auto"><br><span class="input" data-tooltip="Click on text to copy to clipboard">user@computer:~$ </span><span class="emph">type -p python3</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">/usr/bin/python3</span><br> <span class="input" data-tooltip="Click on text to copy to clipboard">user@computer:~$ </span><span class="emph">/usr/bin/python3</span><br> <span class="input" data-tooltip="Click on text to copy to clipboard">Python 3.7.5 (default, Nov 20 2019, 09:21:52)</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">[GCC 9.2.1 20191008] on linux</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">Type "help", "copyright", "credits" or "license" for more information.</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">>>> </span><span class="emph">import uno</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">>>> </span><span class="emph">dir(uno)</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">['Any', 'Bool', 'ByteSequence', 'Char', 'Enum', 'PY2', 'Type', '_ConstantGroup', '__builtin__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_builtin_import', '_component_context', '_impl_getConstantGroupByName', '_uno_extract_printable_stacktrace', '_uno_import', '_uno_struct__eq__', '_uno_struct__getattr__', '_uno_struct__init__', '_uno_struct__ne__', '_uno_struct__repr__', '_uno_struct__setattr__', '_uno_struct__str__', 'absolutize', 'createUnoStruct', 'fileUrlToSystemPath', 'generateUuid', 'getClass', 'getComponentContext', 'getConstantByName', 'getCurrentContext', 'getTypeByName', 'invoke', 'isInterface', 'os', 'pyuno', 'setCurrentContext', 'six_string_types', 'socket', 'sys', 'systemPathToFileUrl', 'traceback', 'warnings']</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">>>> </span><span class="emph">exit()</span><br><span class="input" data-tooltip="Click on text to copy to clipboard">user@computer:~$</span></p>
</span>
</span>
<h2 id="hd_id311633513620803" dir="auto">Alternative console</h2>
<p id="par_id81632760673283" class="paragraph" dir="auto">Use <a target="_blank" href="https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-Python">APSO extension</a> console as an alternative:</p>
<p id="N0144" class="paragraph" dir="auto">
<img src="media/helpimg/python/python_shell.png" class="genericimage" alt="APSO console" style="width:;height:" itemprop="image" itemscope="true" itemtype="http://schema.org/ImageObject">
</p>
<a name="relatedtopics"></a><div class="relatedtopics">
<p class="related" itemprop="mentions" dir="auto"><a name="related"></a><span class="emph">Related Topics</span>
</p>
<div class="relatedbody" itemprop="mentions">
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/python/python_examples.html">Python programming examples</a></p></div>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/python/main0000.html">LibreOffice Python Scripts Help</a></p></div>
<p id="par_id351633599611244" class="paragraph" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03/sf_exception.html#PythonShell"><span class="literal">PythonShell</span></a> function in <a target="_top" href="en-US/text/sbasic/shared/03/sf_exception.html"><span class="literal">ScriptForge.Exception</span></a> service</p>
</div>
</div>
</div>
<div id="DonationFrame"></div>
<footer><div id="DEBUG" class="debug">
<h3 class="bug">Help content debug info:</h3>
<p dir="auto">This page is: <a href="https://opengrok.libreoffice.org/xref/help/source/text/sbasic/python/python_shell.xhp" target="_blank">/text/sbasic/python/python_shell.xhp</a></p>
<p dir="auto">Title is: Python Interactive Shell</p>
<p id="bm_module" dir="auto"></p>
<p id="bm_system" dir="auto"></p>
<p id="bm_HID" dir="auto"></p>
</div></footer>
</body>
</html>